//index.js
//获取应用实例
var app = getApp()
var admin_id = app.globalData.admin_id;
var cat_id = 0;
var share = app.globalData.share;
var left = 250;
Page({
	data: {
		nav: [
			{
				id: 1,
				name: '项目分类',
				click: '',
				original_img: ''
			},
			{
				id: 2,
				name: '环境界面',
				click: '',
				original_img: ''
			},
			{
				id: 3,
				name: '联系方式',
				click: '',
				original_img: ''
			},
			{
				id: 4,
				name: '在线预约',
				click: '',
				original_img: ''
			}
		],
		curID: 's1',
		curAct: 0,
    lenghtforbanner:0
	},
	onLoad: function () {
		var that = this;
		console.log("管理员ID ====" + admin_id);
		//轮播分类ID
		// app.funs.banner(this,4731);
		wx.request({
			url: app.globalData.host + 'index.php/api/goods/classify?admin_id=' + admin_id + '&parent_id=' + app.globalData.lunbo,
			success: function (e) {
				that.setData({
					banner: e.data.data,
				})
			}
		})
		wx.request({
			url: app.globalData.host + 'index.php/api/goods/classify?admin_id=' + admin_id + '&parent_id=' + app.globalData.classify,
			success: function (e) {
				that.setData({
					nav: e.data.data,
				})
			}
		})
		wx.request({
			url: app.globalData.host + 'index.php/api/goods/goodsInfo?admin_id=' + admin_id,
			success: function (e) {
				
				cat_id = app.globalData.huodong;
				that.setData({
					banneritems: e.data.result.filter(that.filterHeaderCatid),
				})
				wx.getSystemInfo({
					success: function (res) {
						that.setData({
							banneritems: e.data.result.filter(that.filterHeaderCatid),
							left: 0,
              lenghtforbanner: e.data.result.filter(that.filterHeaderCatid).length
						})
						left = parseInt(res.windowWidth * 0.64)
					},
				})
				cat_id = app.globalData.news;
				that.setData({
					news: e.data.result.filter(that.filterHeaderCatid),
				})

        cat_id = 67113;
        var arrs = e.data.result.filter(that.filterHeaderCatid);
        for (var i = 0; i < arrs.length; i++) {
          arrs[i].goods_name = arrs[i].goods_name.split('-')
        }

        that.setData({
          teacher: arrs,
        })


			}
		})
    this.getCoupan()
		app.getIntB(function () {
			that.setData({
				intB: app.globalData.intB,
				sw_int: app.globalData.sw_int
			})
		})
		app.checkUser(app.globalData.openid, that)
	},
	filterHeaderCatid: function (e) {
		return (e.cat_id == cat_id)
	},
	goodsClick: function (e) {
		console.log(e)
		wx.navigateTo({
			url: '../../pages/details/details?id=' + e.currentTarget.dataset.id,
		})
	},
	onShareAppMessage: function () {
		return {
			title: share,
			path: 'pages/index/index'
		}
	},
	zhuanti: function (e) {
		console.log(e)
		this.setData({
			curAct: parseInt(e.detail.scrollLeft / left)
		})
	},
	toList: function (e) {
		wx.navigateTo({
			url: '../../pages/goodsList/goodsList?id=' + e.currentTarget.dataset.id + '&name=' + e.currentTarget.dataset.name,
		})
	},
	toDes:function(e){
		wx.navigateTo({
			url: '../../pages/details/details?id=' + e.currentTarget.dataset.id,
		})
	},
  getCoupan(){
    var that = this;
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/Api/coupon?admin_id=' + admin_id,
      success: function (e) {
        console.log(e)
        that.setData({
          items: e.data
        })
        that.fomat()
        that.setData({
          all: that.data.items
        })
        that.check()
      }
    })
  },
  fomat: function () {
    if (!this.data.items) {
      return false
    }
    var arr = this.data.items;
    for (var i = 0; i < arr.length; i++) {
      arr[i].use_start_time = arr[i].use_start_time.split(' ')[0]
      arr[i].use_end_time = arr[i].use_end_time.split(' ')[0]
    }
    this.setData({
      items: arr
    })
  },
  goodsClicks(e) {
    console.log(e.detail.id)
    wx.navigateTo({
      url: '../../pages/goods/goods?id=' + e.detail.id + '&type=' + e.currentTarget.dataset.type,
    })
  },
  tozuoping(e){
    wx.navigateTo({
      url: '../../pages/list/list?id=' + e.currentTarget.dataset.id,
    })
  },
  check: function () {

    if (this.data.items.length <= 0) {
      this.setData({
        isnull: true
      })
    } else {
      this.setData({
        isnull: false
      })
    }
  },
  search(keyword){
    console.log(keyword.detail)
    wx.navigateTo({
      url: '../../pages/searchfor/searchfor?keyword=' + keyword.detail,
    })
  },
  getcard: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/Api/coupon/get?admin_id=' + admin_id + '&cid=' + e.currentTarget.dataset.id + '&openid=' + app.globalData.openid,
      success: function (e) {
        console.log(e)
        wx.showToast({
          title: e.data.msg,
        })
      }
    })
  },
})
